package com.crm.system.service.impl.customerManager;

import com.crm.common.util.WebUtil;
import com.crm.system.service.customerManager.QccImportService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuanyunfeng
 * @date 2021/4/26 14:20
 */
@Service
@Slf4j
public class QccImportServiceImpl implements QccImportService {


    /**
     * 根据客户名称进行企查查导入
     *
     * @param
     * @return
     * @author yuanyunfeng
     * @date 2021/4/22 9:48
     */
    @Override
    public String qccImport(String customerName) throws UnsupportedEncodingException {
        String key = URLEncoder.encode(customerName, "utf-8");
        String urlReq = "https://www.qcc.com/web/search?key=" + key;
//        Map<String, String> resultMap = WebUtil.jsoupDemo(urlReq);
        String linkClassName = "maininfo";
        Map<String, String> cookieUrlMap = WebUtil.getJumpUrl(urlReq,linkClassName);
        String cookie = cookieUrlMap.get("cookie");
        String url = cookieUrlMap.get("url");
        String html = WebUtil.getPageContent(url, cookie,"www.qcc.com");
        Map<String, String> resultMap = WebUtil.resolveHtml(html,"panel b-a base_info");
        //注册地址进行特殊处理
        String value = resultMap.get("注册地址");
        String address = value.substring(0, value.indexOf(" "));
        resultMap.put("注册地址",address);
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        return new Gson().toJson(resultMap);
    }



    @Override
    public String qccByWebsite(String website) {
        log.info("调用企查查网址导入的方法，请求地址是：" + website);
        //根据输入的网址进行企查查爬虫
        String cookie = WebUtil.getCookie(website);
        String html = WebUtil.getPageContent(website, cookie,"www.qcc.com");
        Map<String, String> resultMap = WebUtil.resolveHtml(html,"panel b-a base_info");
        //注册地址进行特殊处理
        String value = resultMap.get("注册地址");
        String address = value.substring(0, value.indexOf(" "));
        resultMap.put("注册地址",address);
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        return new Gson().toJson(resultMap);
    }


    @Override
    public String shuidiImport(String customerName) throws UnsupportedEncodingException {
        String key = URLEncoder.encode(customerName,"UTF-8");
        String urlReq = "https://shuidi.cn/pc-search?key=" + key;
        String linkClassName = "or_search_row_content";
        Map<String, String> cookieUrlMap = WebUtil.getJumpUrl(urlReq,linkClassName);
        String cookie = cookieUrlMap.get("cookie");
        String url = "https://shuidi.cn" + cookieUrlMap.get("url");
        System.out.println("打印水滴拼接之后的网址是："+url);
        String html = WebUtil.getPageContent(url, cookie,"shuidi.cn");
        Map<String, String> resultMap = WebUtil.resolveHtml(html,"detail-info");
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        //法定代表人需要做特殊处理
        String person = resultMap.get("法定代表人");
        String personValue = person.substring(0, person.indexOf(" "));
        resultMap.put("法定代表人",personValue);
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        return new Gson().toJson(resultMap);
    }

    @Override
    public String shuidiByWebsite(String website){
        log.info("调用水滴网址导入的方法，请求地址是：" + website);
        //根据输入的网址进行企查查爬虫
        String cookie = WebUtil.getCookie(website);
        String html = WebUtil.getPageContent(website, cookie,"shuidi.cn");
        Map<String, String> resultMap = WebUtil.resolveHtml(html,"detail-info");
        //法定代表人需要做特殊处理
        String person = resultMap.get("法定代表人");
        String personValue = person.substring(0, person.indexOf(" "));
        resultMap.put("法定代表人",personValue);
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        return new Gson().toJson(resultMap);
    }

    @Override
    public String aiqichaImport(String customerName) throws UnsupportedEncodingException {
        String key = URLEncoder.encode(customerName,"UTF-8");
        String urlReq = "https://aiqicha.baidu.com/s?q="+ key + "&t=0" ;
        String linkClassName = "title";
        //TODO 获取不了链接(返回的查询块被加密)
        Map<String, String> cookieUrlMap = WebUtil.getJumpUrl(urlReq,linkClassName);
        String cookie = cookieUrlMap.get("cookie");
        String url = "https://aiqicha.baidu.com" + cookieUrlMap.get("url");
        System.out.println("打印爱企查拼接之后的网址是："+url);
        String html = WebUtil.getPageContent(url, cookie,"aiqicha.baidu.com");
        Map<String, String> resultMap = WebUtil.resolveHtml(html,"zx-detail-basic-table");
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        return new Gson().toJson(resultMap);
    }

    @Override
    public String aiqichaByWebsite(String website) {
        log.info("调用爱企查网址导入的方法，请求地址是：" + website);
        //根据输入的网址进行企查查爬虫
        String cookie = WebUtil.getCookie(website);
        String html = WebUtil.getPageContent(website, cookie,"aiqicha.baidu.com");
        //TODO 无法获取html文件
        Map<String, String> resultMap = WebUtil.resolveHtml(html,"basic-tab");
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        return new Gson().toJson(resultMap);
    }


    @Override
    public String lvdunImport(String customerName) throws UnsupportedEncodingException {
        String key = URLEncoder.encode(customerName,"UTF-8");
        String urlReq = "https://www.11315.com/newsearch?regionMc=%E9%80%89%E6%8B%A9%E5%9C%B0%E5%8C%BA&regionDm=&searchType=1&searchTypeHead=1&name=" + key;
        String linkClassName = "s_c_content fl";
        Map<String, String> cookieUrlMap = WebUtil.getJumpUrl(urlReq,linkClassName);
        String cookie = cookieUrlMap.get("cookie");
        String url = cookieUrlMap.get("url");
        String html = WebUtil.getPageContent(url, cookie,null);
        Map<String, String> resultMap = WebUtil.resolveLDHtml(html,"content");
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        //注册地址、单位简介 需要做特殊处理
        String add = resultMap.get("注册地址");
        String addressValue = add.substring(0, add.indexOf(" "));
        resultMap.put("注册地址",addressValue);
        String intro = resultMap.get("单位简介");
        String introValue = intro.substring(0, intro.indexOf(" "));
        resultMap.put("单位简介",introValue);
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        return new Gson().toJson(resultMap);
    }

    @Override
    public String lvdunByWebsite(String website) {
        log.info("调用绿盾网址导入的方法，请求地址是：" + website);
        String cookie = WebUtil.getCookie(website);
        String html = WebUtil.getPageContent(website, cookie,null);
        Map<String, String> resultMap = WebUtil.resolveLDHtml(html,"content");
        //注册地址、单位简介 需要做特殊处理
        String add = resultMap.get("注册地址");
        String addressValue = add.substring(0, add.indexOf(" "));
        resultMap.put("注册地址",addressValue);
        String intro = resultMap.get("单位简介");
        String introValue = intro.substring(0, intro.indexOf(" "));
        resultMap.put("单位简介",introValue);
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
        return new Gson().toJson(resultMap);
    }


}
